#include "SchoolSportMeeting.h"


SchoolSportMeeting::SchoolSportMeeting(QWidget *parent)
    : QWidget(parent)
{
    // 设置
    this->setWindowIcon(QIcon("Image\\运动会.svg"));
    QPixmap pixmap("Image\\光标.svg");
    //pixmap.scaled(40, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
    QCursor cursor(pixmap);
    this->setCursor(cursor);

    //QPainter painter(this);
    //painter.fillRect(rect(), Qt::blue);
    //QPixmap bkgnd(":/image/image/背景.jpg");
    //if (bkgnd.isNull()) {
    //    qDebug() << "Image not found!";
    //}
    //// 自动缩放图片到窗口大小
    //bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    //painter.drawPixmap(0, 0, bkgnd);
    
    //this->setAutoFillBackground(true);

    this->verCode = new VerificationCode(this);
    //verCode->setBaseSize(150,50);
    verCode->setGeometry(315, 275,140,45);
    //update();
    this->passwordLineEdit = new PasswordLineEdit(this);
    passwordLineEdit->setGeometry(200,210,225,35);
    
    ui.setupUi(this);
    //QString fileName = "C:/Users/lsh20/Desktop/c++design/c++course/schoolInfo.txt";
    QString fileName = "../schoolInfo.txt";
    loadFile(schoolName,passwords,question,anwser,fileName);
    //初始化显示的学校信息
    updateSchool();
    // 连接信号与槽
    connect(ui.login, &QPushButton::clicked, this, &SchoolSportMeeting::logIn);
    connect(ui.register_2, &QPushButton::clicked, this, &SchoolSportMeeting::register_2);
    connect(ui.forget, &QPushButton::clicked, this, &SchoolSportMeeting::forgetPassword);
    connect(ui.explain, &QPushButton::clicked, this, &SchoolSportMeeting::explain);
    connect(ui.pushButton, &QPushButton::clicked, this, &SchoolSportMeeting::updateSchool);
}

SchoolSportMeeting::~SchoolSportMeeting()
{
    delete verCode;
}
// 弹窗
void message(QString title, QString text, QMessageBox::Icon icon, QMessageBox::Button buttonType, QWidget* parent) {
    QMessageBox msgBox(parent);  
    msgBox.setWindowTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(icon);
    msgBox.setStandardButtons(buttonType);
    msgBox.exec();
}
// 问答弹窗
int messageQuestion(QString title, QString text, QMessageBox::StandardButton stdBtn1, QMessageBox::StandardButton stdBtn2, QWidget* parent) {
    int ret = QMessageBox::question(parent, title, text, stdBtn1, stdBtn2);
    return ret;
}
// 登录
void SchoolSportMeeting::logIn() {
    password = passwordLineEdit->text();
    code = ui.vercode->text();
    if (password.isEmpty() || code.isEmpty()) {
          message("提示", "密码和验证码不能为空", QMessageBox::Icon::Warning, QMessageBox::Ok,this);
    }else if (code == verCode->code) {
            // 登录成功(跳转)
        int currentIndex = ui.schoolBox->currentIndex();

        if (password == passwords[currentIndex]) {
            SportMeeting* sportMeeting = new SportMeeting();
            sportMeeting->show();
            this->close();
        }
        else {
            // 账号密码错误
            QString title = "错误";
            QString text = "学校或者密码错误！！！";
            message(title, text, QMessageBox::Icon::Critical, QMessageBox::Ok,this);
        }
    }
    else {
        //验证码不正确
        QString title = "错误";
        QString text = "验证码输入错误！！！";
        message(tr("错误"), tr("验证码输入错误！！！"), QMessageBox::Icon::Critical, QMessageBox::Ok,this);
    }
}
// 注册
void SchoolSportMeeting::register_2() {
    // 注册窗口
    LogIn* loginWidget = new LogIn;
    loginWidget->setAttribute(Qt::WA_ShowModal, true);
    loginWidget->show();
    
    // 添加学校
    //QString school = schoolName.back();
    //ui.schoolBox->addItem(school);
        
    // 设置密码

    // 确认密码

}
// 忘记密码
void SchoolSportMeeting::forgetPassword() {
    forget* forgetWidget = new forget;
    forgetWidget->setAttribute(Qt::WA_ShowModal, true);
    forgetWidget->show();
}
// 使用说明
void SchoolSportMeeting::explain() {
    // 使用说明窗口
    InstructionsForUse* instructionWidget = new InstructionsForUse;
    instructionWidget->show();
}

//更新注册的学校信息
void SchoolSportMeeting::updateSchool() {
    ui.schoolBox->clear();
    for (const auto& item : schoolName) {
        ui.schoolBox->addItem(item);
    }
}
void SchoolSportMeeting::paintEvent(QPaintEvent* event) {
    QPainter painter(this);
    QString imagePath = "image\\背景1.jpg";

    // 检查路径是否存在
    if (!QFile::exists(imagePath)) {
        qDebug() << "图片文件不存在：" << imagePath;
    }
    else {
        QPixmap bkgnd(imagePath);
        if (bkgnd.isNull()) {
            qDebug() << "无法加载图片：" << imagePath;
            painter.fillRect(rect(), QColor("#2c3e50"));

            painter.setPen(Qt::white);
            painter.drawText(rect(), Qt::AlignCenter, "背景加载失败");
            return;
        }
        // 确认窗口有效尺寸
        if (width() <= 0 || height() <= 0) {
            qDebug() << "无效窗口尺寸：" << size();
            return;
        }

        // 高质量缩放
        bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        //painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
        //painter.fillRect(rect(), QColor(0, 0, 0, 200)); // 半透明黑色覆盖层
        painter.setOpacity(0.7);  //0.7
        painter.drawPixmap(0, 0, bkgnd);
    }

}
void setBtnStyle(QPushButton* btn) {
    btn->setStyleSheet("");
}